package com.tjy.web.controller;

import com.tjy.bean.*;
import com.tjy.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class MobilePhoneController {
    @Autowired
    private ILoginService loginService;

    @RequestMapping("/toLogin.do")
    public String toLogin(User user , Model model , HttpSession session){
       User userInfo= loginService.toLogin(user);
       //如果user不为空设置session信息 跳转到首页
        if(userInfo!=null){
            session.setAttribute("user",userInfo);
            return "redirect:/toMobilePhone.do";
        }else{
            model.addAttribute("bug","账户或密码有误");
            return "login";
        }
    }


    @Autowired
    private IIndexService iIndexService;//注入查询品牌service层

    @Autowired
    private IMobilePhoneByPageService mobilePhoneByPageService;//注入分页service层

    @RequestMapping("/toMobilePhone.do")
    public String toMobilePhone(Model model, Integer currentPage ,Integer id  ) {
        List<MobileType> mobileType=   iIndexService.selectMobileType();
        model.addAttribute("mobileType", mobileType);
        if(id!=null){
            List<MobilePhone> mobilePhones = iIndexService.selectOneTypePhone(id);
            model.addAttribute("mobilePhones", mobilePhones);
        }else{
            //当前页数为0默认为第一页
            if (null == currentPage ) {
                currentPage = 1;
            }
            //查询分页的数据设置到model跳转页面
            PageBean pageBean = mobilePhoneByPageService.selectMobilePhoneByPage(currentPage);
            model.addAttribute("pageBean", pageBean);
        }

        return "index";
    }

    @Autowired
    private IBasicSituationService basicSituationService;//注入第一步service
    @Autowired
    private IFunctionSituationService functionSituationService;//注入第二步
    @Autowired
    private IWearDegreeService wearDegreeService;//注入第三步
    @Autowired
    private IMonthTopPriceService monthTopPriceService;//注入30天内最高回收价格

    @RequestMapping("/toInfo.do")
    public String toInfo(Model model, Integer id) {
        //第一步
        List<BasicSituation> firstList = basicSituationService.selectBasicSituation(id);
        model.addAttribute("firstList", firstList);
        //第二步
        List<FunctionSituation> secondList = functionSituationService.selectFunctionBasicSituation(id);
        model.addAttribute("secondList", secondList);
        //第三步
        List<WearDegree> thirdList = wearDegreeService.selectWearDegree(id);
        model.addAttribute("thirdList", thirdList);
        //三十天内最高回收价格
        Integer monthTopPrice = monthTopPriceService.selectMonthTopPrice(id);
        model.addAttribute("monthTopPrice", monthTopPrice);
        //内连接查询手机及手机品牌信息
       MobilePhone mobilePhone = monthTopPriceService.selectMobilePhone(id);
        model.addAttribute("mobilePhone", mobilePhone);
        //传递手机id做曲线图
        model.addAttribute("fromId", id);
        return "info";
    }
@RequestMapping("/toFourMonth.do")
@ResponseBody
    public  List<FourMonthMaxPrice> toFourMonth(Integer id){
    List<FourMonthMaxPrice> fourMonthMaxPrices = monthTopPriceService.selectFourMonth(id);
    //集合数据倒叙排列 10月----1月排列
    Collections.reverse(fourMonthMaxPrices);
    return fourMonthMaxPrices;
}

    @Autowired
    private IPriceService priceService;

    @RequestMapping("/toCountPrice.do")
    public String toCountPrice(Integer[] property1, Integer[] property2, Integer[] property3,Model model,Integer fromId) {
        Double down1=0.0;//定义变量接收 第一次估算一共降价了多少
        Double firstPrice=0.0;//定义变量接收 第一次估算后手机的价格
        Double down2=0.0;//定义变量接收 第二次估算一共降价了多少
        Double secondPrice =0.0;//定义变量接收 第二次估算后手机的价格
        String sc1 = null;
        String sc2 = null;
        String sc3 = null;
        ShoppingCart shoppingCart1 = new ShoppingCart();

        Integer price = priceService.selectPrice(fromId);//手机id拿价格
        for (Integer p1 : property1) {
            String s1 = priceService.selectBasicSituation(p1);
            sc1=sc1+s1+" ";
            Double weight = priceService.selectWeight(p1);//拿到第一次每个参数对应的降价率
            //判断如果降价率等于0就不用计算了
            if(weight!=0.0 && weight!=null){
                //求出第一次一共降价多少钱
                Double downPrice =price*weight;
                down1 = down1+ downPrice;
                //总价钱减去第一次降价得到第一次估算价格
                firstPrice=price-down1;
            }
        }
        String substring1 = sc1.substring(4);
//        shoppingCart1.setBasicSituation(substring1);
            model.addAttribute("substring1",substring1);
        for (Integer p2:property2) {
            if(p2 != 0){
                String s2 = priceService.selectFunctionSituation(p2);
                sc2=sc2+s2+" ";
            }

        }
        String substring2 = sc2.substring(4);
        model.addAttribute("substring2",substring2);
        for (Integer p3: property3) {
            String s3 = priceService.selectWearDegree(p3);
            sc3=sc3+s3+" ";
            Double  weights =  priceService.selectWeights(p3);//拿到第二次的降价率
            //如果第一次估算价格不为0
            if(firstPrice!=0.0 && firstPrice != null){
                //求出第二次一共降价多少钱
                Double downPrice =   firstPrice*weights;
                down2 = down2 + downPrice;
                //用第一次估算的价格减去 第二次降价的价格得到最总估价的价格
                secondPrice=firstPrice-down2;
                //如果第一次估算价格为0
            }else{
                    //拿到原价*第二次降价率 得到一共降价多少钱
                    Double downPrice =price*weights;
                    down2=down2+downPrice;
                    //原价减去降价得到 估算价格
                    secondPrice=price-down2;
            }
        }
        String substring3 = sc3.substring(4);
        model.addAttribute("substring3",substring3);
        //将最后总估算价格传递到估算页面
        model.addAttribute("price",secondPrice);
        //查询手机信息
       MobilePhone img = priceService.selectImg(fromId);
        String img1 = img.getImg();
        String mobileName = img.getMobileName();
        model.addAttribute("img",img);
       List<ShoppingCart> shoppingCart= priceService.selectShopping();
        model.addAttribute("shoppingCart",shoppingCart);
       Integer count = priceService.selectCount();
        model.addAttribute("count",count);

//        shoppingCart1.setImg(img1);
//        shoppingCart1.setMobileName(mobileName);
//        shoppingCart1.setPrice(secondPrice);
//        model.addAttribute("shoppingCart1",shoppingCart1);
        return "price";
    }



    @Autowired
    private IShoppingService shoppingService;
    @RequestMapping("/Shopping.do")
    @ResponseBody
    public Map toShopping(HttpServletRequest request, String img, String mobileName, Double price, String basicSituation, String functionSituation, String wearDegree){
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setImg(img);
        shoppingCart.setMobileName(mobileName);
        shoppingCart.setWearDegree(wearDegree);
        shoppingCart.setFunctionSituation(functionSituation);
        shoppingCart.setBasicSituation(basicSituation);
        shoppingCart.setPrice(price);
        Integer one =shoppingService.insertShooping(shoppingCart);
        Map<Object, Object> map = new HashMap<>();
        if(one!=0){
            map.put("flag",true);
            List<ShoppingCart> shoppingCart1= priceService.selectShopping();
            map.put("shop",shoppingCart1);
        }else{
            map.put("flag",false);
        }

        return map;
    }
}